﻿using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace CamelotSQLCommander.CamelotSQLCommander
{
    [ToolboxItemAttribute(false)]
    public class CamelotSQLCommander : WebPart
    {
        // Visual Studio might automatically update this path when you change the Visual Web Part project item.
        private const string _ascxPath = @"~/_CONTROLTEMPLATES/CamelotSQLCommander/CamelotSQLCommander/CamelotSQLCommanderUserControl.ascx";

        protected override void CreateChildControls()
        {
            // Webpart settings
            ChromeType = PartChromeType.Default;
            Title = "Camelot SQL Commander";
            Description = "Camelot SQL Commander for SharePoint by Bendsoft. www.bendsoft.com";

            // Set the usercontrol
            var control = Page.LoadControl(_ascxPath) as CamelotSQLCommanderUserControl;
            if(control == null)
                return;

            control.CamelotSqlCommander = this;
            Controls.Add(control);
        }


        #region ConnectionString

        private string serverAddress;
        [Category("ConnectionString"),
        WebBrowsable(true),
        Personalizable(PersonalizationScope.Shared),
        WebDisplayName("Server address"),
        WebDescription("The IP or hostname of the SharePoint server. Required.")]
        public string ServerAddress
        {
            get
            {
                return serverAddress ?? HttpContext.Current.Request.Url.Host;
            }
            set { serverAddress = value; }
        }

        private string userName;
        [Category("ConnectionString"),
        WebBrowsable(true),
        Personalizable(PersonalizationScope.Shared),
        WebDisplayName("User name"),
        WebDescription("User name of the account used to access SharePoint. This account must have sufficient privileges to access the provided site. Required when server uses authentication mode [Ntlm] or [Basic].")]
        public string UserName
        {
            get
            {
                return userName ?? string.Empty;
            }
            set { userName = value; }
        }

        private string userPassword;
        [Category("ConnectionString"),
        WebBrowsable(true),
        Personalizable(PersonalizationScope.Shared),
        WebDisplayName("Password"),
        WebDescription("Password for the actual user")]
        public string UserPassword
        {
            get
            {
                return userPassword ?? string.Empty;
            }
            set { userPassword = value; }
        }

        private string userDomain;
        [Category("ConnectionString"),
        WebBrowsable(true),
        Personalizable(PersonalizationScope.Shared),
        WebDisplayName("User Domain (optional)"),
        WebDescription("The domain of the specified user. Leave empty if domain user account is not used.")]
        public string UserDomain
        {
            get
            {
                return userDomain ?? string.Empty;
            }
            set { userDomain = value; }
        }

        //private string siteName;
        //[Category("ConnectionString"),
        //WebBrowsable(true),
        //Personalizable(PersonalizationScope.Shared),
        //WebDisplayName("Site Name"),
        //WebDescription("Corresponds to the site path on the SharePoint server, e.g. marketing or marketing/news. Leave empty for top site.")]
        //public string SiteName
        //{
        //    get
        //    {
        //        if (siteName != null)
        //            return siteName;
        //        else
        //            return string.Empty;
        //    }
        //    set { siteName = value; }
        //}

        private string authentication;
        [Category("ConnectionString"),
        WebBrowsable(true),
        Personalizable(PersonalizationScope.Shared),
        WebDisplayName("Authentication"),
        WebDescription("Indicates the authentication mode used on the SharePoint server. [Default]: Use the credentials from the current security context. No specific account details should be provided. Only set this mode when SPC is used in components running inside SharePoint, such as webparts. SPC is then running as the current user. [Ntml]: Specifies client authentication using Ntml. This is the most common scenario. [Basic]: Specifies that user authenticates to the server using basic authentication which is a simpler less secured form.")]
        public string Authentication
        {
            get
            {
                return authentication ?? "Default";
            }
            set { authentication = value; }
        }

        private int timeout;
        [Category("ConnectionString"),
        WebBrowsable(true),
        Personalizable(PersonalizationScope.Shared),
        WebDisplayName("Timeout"),
        WebDescription("Connection time out")]
        public int Timeout
        {
            get
            {
                return timeout == 0 ? 60 : timeout;
            }
            set { timeout = value; }
        }

        private int defaultLimit;
        [Category("ConnectionString"),
        WebBrowsable(true),
        Personalizable(PersonalizationScope.Shared),
        WebDisplayName("DefaultLimit"),
        WebDescription("How many list items to handle in each batch, default 100")]
        public int DefaultLimit
        {
            get
            {
                return defaultLimit == 0 ? 100 : defaultLimit;
            }
            set { defaultLimit = value; }
        }



        #endregion


    }
}
